package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.captainbank.tcl.tclapplication.utils.calendar.CalendarEvent;
import com.captainbank.tcl.tclapplication.utils.calendar.CalendarProviderManager;
import com.gyf.barlibrary.ImmersionBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/4/16.
 * Describe:
 */
public class CalendarActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_title)
    EditText etTitle;
    @BindView(R.id.tv_show)
    TextView tvShow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_calendar;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.colorPrimary)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_calendar));
    }

    @OnClick({R.id.btn_clear,R.id.btn_add, R.id.btn_del, R.id.btn_update, R.id.btn_query, R.id.btn_go, R.id.btn_search})
    public void onViewClicked(View view) {
        String title;
        switch (view.getId()) {
            case R.id.btn_clear:
                etTitle.setText("");
                tvShow.setText("");
                break;
                case R.id.btn_add:
                    title = etTitle.getText().toString().trim();
                    if(StringUtil.checkIsNotNull(title)){
                        initCalendar(1,title);
                    }else{
                        MyToast.toastError(CalendarActivity.this,"请输入标题",false);
                    }
                break;
            case R.id.btn_del:
                initCalendar(2,"");
                break;
            case R.id.btn_update:
                title = etTitle.getText().toString().trim();
                if(StringUtil.checkIsNotNull(title)){
                    initCalendar(3,title);
                }else{
                    MyToast.toastError(CalendarActivity.this,"请输入标题",false);
                }
                break;
            case R.id.btn_query:
                initCalendar(4,"");
                break;
            case R.id.btn_go:
                title = etTitle.getText().toString().trim();
                if(StringUtil.checkIsNotNull(title)){
                    initCalendar(5,title);
                }else{
                    MyToast.toastError(CalendarActivity.this,"请输入标题",false);
                }
                break;
            case R.id.btn_search:
                title = etTitle.getText().toString().trim();
                if(StringUtil.checkIsNotNull(title)){
                    initCalendar(6,title);
                }else{
                    MyToast.toastError(CalendarActivity.this,"请输入标题",false);
                }
                break;
        }
    }

    /**
     * @param type 1：增
     *             2：删
     *             3：改
     *             4：查
     */
    private void initCalendar(final int type,final String mTitle) {
        requestPermission(1, new String[]{Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR}, new Runnable() {
            @Override
            public void run() {
                long accountId = CalendarProviderManager.obtainCalendarAccountID(CalendarActivity.this);
                if (accountId != -1) {
                    switch (type) {
                        case 1:
                            CalendarEvent calendarEvent = new CalendarEvent(
                                    mTitle,
                                    "这是一个新建的测试提醒",
                                    "地点：办公室",
                                    System.currentTimeMillis(),
                                    System.currentTimeMillis() + 60000,
                                    0,
                                    null
                            );
                            int addResult = CalendarProviderManager.addCalendarEvent(CalendarActivity.this, calendarEvent);
                            if (addResult == 0) {
                                MyToast.toastSuccess(CalendarActivity.this, "添加成功", false);
                            } else {
                                MyToast.toastError(CalendarActivity.this, "添加失败", false);
                            }
                            break;

                        case 2:
                            List<CalendarEvent> events = CalendarProviderManager.queryAccountEvent(CalendarActivity.this, accountId);
                            if (null != events) {
                                if (events.size() == 0) {
                                    MyToast.toastNormal(CalendarActivity.this, "没有事件可以删除");
                                } else {
                                    long eventID = events.get(0).getId();
                                    int delResult = CalendarProviderManager.deleteCalendarEvent(CalendarActivity.this, eventID);
                                    if (delResult != -2) {
                                        MyToast.toastSuccess(CalendarActivity.this, "删除成功", false);
                                    } else {
                                        MyToast.toastError(CalendarActivity.this, "删除失败", false);
                                    }
                                }
                            } else {
                                MyToast.toastError(CalendarActivity.this, "查询失败", false);
                            }
                            break;

                        case 3:
                            List<CalendarEvent> events2 = CalendarProviderManager.queryAccountEvent(CalendarActivity.this, accountId);
                            if (null != events2) {
                                if (events2.size() == 0) {
                                    MyToast.toastNormal(CalendarActivity.this, "没有事件可以更新");
                                } else {
                                    long eventID = events2.get(0).getId();
                                    int result3 = CalendarProviderManager.updateCalendarEventTitle(CalendarActivity.this, eventID, mTitle);
                                    if (result3 == 1) {
                                        MyToast.toastSuccess(CalendarActivity.this, "更新成功", false);
                                    } else {
                                        MyToast.toastError(CalendarActivity.this, "更新失败", false);
                                    }
                                }
                            } else {
                                MyToast.toastError(CalendarActivity.this, "查询失败", false);
                            }
                            break;

                        case 4:
                            List<CalendarEvent> events3 = CalendarProviderManager.queryAccountEvent(CalendarActivity.this, accountId);
                            StringBuilder sb = new StringBuilder();
                            if (null != events3) {
                                for (CalendarEvent event : events3) {
                                    sb.append(event.toString()).append("\n");
                                }
                                tvShow.setText(sb.toString());
                                MyToast.toastSuccess(CalendarActivity.this, "查询成功", false);
                            } else {
                                MyToast.toastError(CalendarActivity.this, "查询失败", false);
                            }
                            break;

                        case 5:
                            CalendarProviderManager.startCalendarForIntentToInsert(CalendarActivity.this, System.currentTimeMillis(),
                                    System.currentTimeMillis() + 60000, mTitle, "提醒我", "东方海外大厦", false);
                            break;

                        case 6:
                            if (CalendarProviderManager.isEventAlreadyExist(CalendarActivity.this, 1555395931000L,
                                    1557987931000L, mTitle)) {
                                MyToast.toastSuccess(CalendarActivity.this, "存在", false);
                            } else {
                                MyToast.toastError(CalendarActivity.this, "不存在", false);
                            }
                            break;


                        default:
                            break;
                    }
                }
            }
        }, new Runnable() {
            @Override
            public void run() {
                MyToast.toastError(CalendarActivity.this,"没有权限",false);
            }
        });
    }

}
